草庐IT

Java final 与 C++ const

全部标签

c++ - "const"是如何实现的?

C或C++编译器(例如gcc)如何遵守const声明?例如,在下面的代码中,编译器如何跟踪变量ci是const并且不能被修改?intget_foo(){return42;}voidtest(){inti=get_foo();i+=5;constintci=get_foo();//ci+=7;//compileerror:assignmentofread-onlyvariable?ci?} 最佳答案 很像变量的任何其他符号信息(地址、类型等)。通常有一个符号表存储有关已声明标识符的信息,每当遇到对该标识符的另一个引用时都会查询它。(一

c++ - 在 C++ 中更改 const 值的方法

constvalue在编程中经常使用。它为程序员提供了安全性,即该值将来不会改变。不幸的是,这并没有完全执行。因此,它有时会导致难以破译的细微错误。举个例子:inta=1;constint&b=a;//IpromiseIwillnotchangea=3;//Iamaliar,Iamnow3cout也可以用其他方式改变const的值;其中一些非常复杂,需要大量代码,而这些代码通常会在其他代码段中丢失,从而导致错误。因此,有人会详细说明更改const值的可能方法吗?如果您不了解所有内容,请不要担心,很多人不会(包括我自己)。把你知道的所有方法都说出来——毕竟你只能给出你所拥有的!

C++在派生类中初始化基类的const int?

我的基类中有一个常量int变量,我想在我的派生类中用不同的值(作为参数)初始化响应变量,这可能吗?这是我做的://Base.h(methodsimplementedinBase.cppintheactualcode)classBase{public:Base(constintindex):m_index(index){}intgetIndex()const{returnm_index;}private:constintm_index;};//Derived.hclassDerived:publicBase{public:Derived(constintindex,conststd::s

c++ - 强制 foreach 使用 const 迭代器

我有一个自定义容器类,它实现了cbegin()和cend()函数。然后我在foreach循环中使用它,但它似乎需要begin()和end()成员函数,即使我尝试使用const修饰符:for(constautoval:container)像这样:for(autoconstval:container)像这样:for(constautoconstval:container)是否可以强制foreach使用常量c函数? 最佳答案 当然:让范围看起来像一个const范围:templateTconst&make_const(Tconst&argu

c++ - 如何将 const char* 转换为字符串,然后再转换回 char*?

我刚开始使用C++,很难理解constchar*。我正在尝试将方法中的输入转换为string,然后更改字符串以在我想要的位置添加连字符并最终获取该字符串并将其转换回char*返回。到目前为止,当我尝试这个时,它给我一个总线错误10。char*getHyphen(constchar*input){stringvowels[12]={"A","E","I","O","U","Y","a","e","i","o","u","y"};//convertchar*tostringstringa;inti=0;while(input!='\0'){a+=input[i];input++;i++;}

c++ - 为什么复制 const shared_ptr& 不会违反 const-ness?

尽管我的代码编译得很好,但这一直困扰着我,我无法在stackoverflow上找到答案。以下通用构造函数是将shared_ptr传递给构造函数中的类实例的一种方法。MyClass{MyClass(conststd::shared_ptr&pt);std::shared_ptrpt_;//EDITED:Removed&typo};MyClass::MyClass(conststd::shared_ptr&pt):pt_(pt){}这编译得很好。我的问题如下:在我的理解中,像这样声明一个参数const:voidmyfunc(constT&t)promise不改变t。但是,通过将shared

c++ - 如何在不违反 const 正确性的情况下使用 std::lock_guard?

在一个子类中,我有一个私有(private)的std::mutexm字段,我在基类纯虚方法的实现中使用它以线程安全的方式返回一个值(值可以由另一个线程更新):intSubClass::get()const//implements'virtualintget()=0const'ofthebaseclass{std::lock_guardlck(m);returnvalue;}编译器通过产生错误告诉我这违反了const正确性:error:binding'conststd::mutex'toreferenceoftype'std::lock_guard::mutex_type&{akastd

c++ - 返回 const 引用和右值引用之间的区别

如果我没记错的话,我认为const引用和右值引用都可以绑定(bind)到右值。返回前者的函数和返回后者的函数之间有什么实际区别吗?编辑。我不能修改前者,但为什么我会对修改右值感兴趣?有道理吗? 最佳答案 const左值引用可以绑定(bind)到任何东西。右值引用只能绑定(bind)到非const右值。non-constlvalueconstlvaluenon-constrvalueconstrvalueconstT&yesyesyesyesT&&nonoyesno如您所见,它们非常不同。此外,如果函数调用返回左值引用,则该表达式为左

c++ - 不存在从 "std::string"到 "const char *"的合适转换函数

我正在尝试删除.txt文件,但文件名存储在类型为std::string的变量中。问题是,程序事先不知道文件名,所以我不能只使用remove("filename.txt");stringfileName2="loInt"+fileNumber+".txt";基本上我想做的是:remove(fileName2);但是,它告诉我不能使用它,因为它给我错误:Nosuitableconversionfunctionfrom"std::string"to"constchar*"exists. 最佳答案 remove(fileName2.c_st

c++ - 返回 const 引用与临时对象

我想知道为什么返回本地对象的constreference是非法的,而返回localobject是合法的,只要你将它分配给const引用?vectorf_legal(){vectortempVec;tempVec.push_back(1);returntempVec;}constvector&f_illegal(){vectortempVec;tempVec.push_back(1);returntempVec;}voidg(){constvector&v1=f_legal();//legalconstvector&v2=f_illegal();//illegal}编辑:我的观点是,如果